package com.cafuc.base;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringOperation {

    public void stringTest() {
        String aa = "cdsacdscd,fefrew,cdsacds,frewgre";
        System.out.println(aa.length());
        System.out.println(aa.indexOf(","));
        System.out.println(aa.replace("a", "***"));
        System.out.println(aa.replaceAll("a", "***"));
        System.out.println(Arrays.asList(aa.split(",")));
        System.out.println(aa.substring(0, 10));

        StringBuffer sb = new StringBuffer();
        sb.append("hj").append("cdscdasc").append(11);
        System.out.println(sb.toString());

        StringBuilder sb1 = new StringBuilder();
        sb1.append("aaaaa").append("cdscdasc").append(11);
        System.out.println(sb1.toString());

        StringTokenizer st = new StringTokenizer(aa, ",");
        while(st.hasMoreTokens()) {
            System.out.println(st.nextToken());
        }

        if (StringUtils.isNotBlank(aa)) {
            System.out.println("cdsacdascdas");
        }
        // == equals
        int i = 1;
        int j = 1;
        String a = "sun";
        String b = "sun";
        String c = new String("sun");
        String d = new String("sun");
        System.out.println(i == j); // true
        System.out.println(a == b); // true
        System.out.println(a == c); // false
        System.out.println(c == d); // false
        System.out.println(a.equals(c)); // true

        // 正则表达式
        String emailRegex = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        String email = "1320697114@qq.com";
        System.out.println(email.matches(emailRegex));
        Matcher matcher= Pattern.compile(emailRegex).matcher(email);
        System.out.println(matcher.matches());
        // 2021-12-31
        Matcher matcher1= Pattern.compile("(\\d+)-(\\d+)-(\\d+)").matcher("2021-12-31");
        if (matcher1.find()) {
            System.out.println(matcher1.group(0));
            System.out.println(matcher1.group(1));
            System.out.println(matcher1.group(2));
            System.out.println(matcher1.group(3));
        }
        System.out.println(email.replaceAll("1.*?@", "1***@"));
    }

    public static void main(String[] args) {
        StringOperation so = new StringOperation();
        so.stringTest();
    }
}
